package battle.map
{
	public class Map
	{
		public static  const TILE_WIDTH:int = 32;
		public static const TILE_HEIGHT:int = 32;
		
		public var mapId:int;
		
		public var width:int;
		public var height:int;
		
		public var cols:int;
		public var rows:int;
		public var totalTiles:int;
		
		
		public var layerNum:int;
		
		public var layers:Vector.<Vector.<Tile>>;
		
		/*
		//地表层
		public var layer1:Vector.<Tile>;
		
		//景物层
		public var layer2:Vector.<Tile>;
		
		//遮罩层
		public var layer3:Vector.<Tile>;
		*/
		
		//碰撞
		public var collisions:Vector.<int>;
		
		
		public var tilesetIDs:Vector.<String> = new Vector.<String>();
		
		public function Map()
		{
		}
		
		
		public function loadMap( data:String ):void
		{
			var lines:Array = data.split( "\r\n" );
			
			var head:Array = String( lines[0] ).split( "," );
			
			var index:int = 0;
			
			this.cols = int( head[index++] );
			
			this.rows = int( head[index++] );
			
			this.totalTiles = this.cols * this.rows;
			
			this.width = this.cols * Map.TILE_WIDTH;
			
			this.height = this.rows * Map.TILE_HEIGHT;
			
			this.layerNum  = int( head[index++] );
			
			var tileSetNum:int = int( head[index++] );
			
			//var tilesetImgs:Vector.< String > = new Vector.<String>();
			
			for( var i:int = 0; i < tileSetNum; i++ )
			{
				//tilesetImgs.push( "battle/map/tilesets/" + head[ index++ ] + ".png" );
				
				tilesetIDs.push( head[index++] );
				
			}
			
			//layer1 = new Vector.<Tile>();
		
			var lineIndex:int = 1;
		
			layers = new Vector.<Vector.<Tile>>();
			
			for( var k:int = 0; k < this.layerNum; k++ )
			{
				layers[k] = new Vector.<Tile>();
				
				for( var i:int = 0; i < rows; i++ )
				{
					var line:Array = String( lines[ lineIndex++ ] ).split( "," );
					
					for( var j:int = 0; j < cols; j++ )
					{
						
						var tile:Tile = null;
						
						if( line[j] != "0" )
						{
							
							tile =Tile.newTile( line[j] );
						}
						
						layers[k].push( tile );	
					}
				}
				
			}
			
			collisions = new Vector.<int>();
			
			for( var i:int = 0; i < rows; i++ )
			{
				var line:Array = String( lines[ lineIndex++ ] ).split( "," );
				
				for( var j:int = 0; j < cols; j++ )
				{
					var walkable:int = int( line[j] );
					
					collisions.push( walkable );
				}
			}
			
		}
		
		
		
	}
}